import 'dart:async';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';

class WankeData {
  final tableName;
  Database db;
  WankeData({this.tableName});
  Future<Database> iniSqlite() async {
    var databasesPath = await getDatabasesPath();
    String path = join(databasesPath, "wanke.db");
    return this.open(path);
  }

  Future<Database> open(String path) async {
    db = await openDatabase(path, version: 1, onCreate: (Database db, int version) async {
      await db.execute('''
create table groups ( 
  id integer primary key autoincrement, 
  name text not null
);
''');
      await db.execute('''
create table devices (
  id integer primary key autoincrement,
  name text,
  group_id integer not null,
  account text not null,
  password text not null,
  deviceid text not null,
  imeiid text not null,
  sessionid text not null,
  userid text not null,
  device_name text,
  capacity_1 text,
  used_1 text,
  capacity_2 text,
  used_2 text,
  addr text,
  device_id text,
  device_sn text,
  peerid text,
  system_version text,
  ip_addr text,
  mac_address text,
  lan_ip text,
  city text,
  province text,
  isp text,
  status text,
  upnp text,
  bind_time text,
  bind_days text,
  hardware_model text,
  is_bind text,
  iswanke text,
  ispush text,
  yesterday_income real,
  prevday_income real,
  balance_income real,
  total_income real,
  income_day text
);
''');
      await db.execute('''
CREATE TABLE income (
"id"  INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"income"  real,
"date"  text,
"device_id"  integer
);
''');
      await db.execute('''
CREATE TABLE block (
"id"  INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"addr"  text,
"balance"  real,
"count"  integer,
"last" integer,
"note" text
);
''');
      await db.execute('''INSERT INTO groups ("name") VALUES ('默认分组')''');
    });
    return db;
  }

  Future<int> insert(Map<String, dynamic> data) async {
    int id = await db.insert(tableName, data);
    return id;
  }
  Future<Map> find(int id) async {
    List<Map> maps = await db.query(tableName, where: "id=?", whereArgs: [id]);
    if (maps.length == 0) {
      return null;
    } else {
      return maps.first;
    }
  }

  Future<List> select() async {
    List<Map> rows = await db.query(tableName);

    if (rows.length == 0) {
      return null;
    } else {
      return rows;
    }
  }

  Future<int> rowCount() async {
    List<Map> rows = await db.query(tableName);
    return rows.length;
  }

  Future<List> selectByColumn(String column, dynamic arg) async {
    List<Map> rows = await db.query(tableName, where: "$column=?", whereArgs: [arg]);
    if (rows.length == 0) {
      return null;
    } else {
      return rows;
    }
  }

  Future<Map> findByColumn(String column, String arg) async {
    List<Map> rows = await db.query(tableName, where: "$column=?", whereArgs: [arg]);
    if (rows.length == 0) {
      return null;
    } else {
      return rows.first;
    }
  }

  Future<int> deleteByColumn(String column, String arg) async {
    return await db.delete(tableName, where: "$column = ?", whereArgs: [arg]);
  }

  Future<int> update(Map<String, dynamic> data, String whereColumn, String whereValue) async {
    return await db.update(tableName, data, where: "$whereColumn = ?", whereArgs: [whereValue]);
  }

  Future close() async => db.close();
}
